//
//  DFWHomeOptionalView.m
//  DecxForward
//
//  Created by 程强 on 16/10/18.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DFWHomeOptionalView.h"

@interface DFWHomeOptionalView()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *optionalTable;
@end

@implementation DFWHomeOptionalView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); //tableview旋转之后的位置
        self.optionalTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width) style:UITableViewStylePlain];
        _optionalTable.delegate = self;
        _optionalTable.dataSource = self;
        [self addSubview:_optionalTable];
        _optionalTable.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        _optionalTable.center = CGPointMake(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height/2);
        _optionalTable.showsVerticalScrollIndicator = NO;
        _optionalTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"OptionalCell";
    
    DFWOptionalCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DFWOptionalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.transform =CGAffineTransformMakeRotation(M_PI / 2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DDGoodsObj *obj = self.dataAry[indexPath.row];
    [cell.goodsIcon sd_setImageWithURL:[NSURL URLWithString:obj.thumbnail] placeholderImage:nil];
    cell.priceLbl.text = FormatString(@"$%@", obj.price);
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(optionalView:didSelectIndex:)]) {
        [self.delegate optionalView:self didSelectIndex:indexPath.row];
    }
}
- (void)setDataAry:(NSArray *)dataAry
{
    _dataAry = dataAry;
    [self.optionalTable reloadData];
}
- (void)reloadData
{
    [self.optionalTable reloadData];
}
@end

@implementation DFWOptionalCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            
        self.goodsIcon = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 120, 120)];
        _goodsIcon.image = LoadImage(@"category_test");
        [self.contentView addSubview:_goodsIcon];
        
        self.priceLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"$200",kRedColor,[UIFont systemFontOfSize:15.0],NSTextAlignmentCenter).laFrame(CGRectMake(5, 130, 120, 25));
        }];
        [self.contentView addSubview:_priceLbl];
    }
    return self;
}
@end
